package com.grass.finding.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.grass.finding.common.ErrorCode;
import com.grass.finding.exception.BusinessException;
import com.grass.finding.model.domain.User;
import com.grass.finding.service.UserService;
import com.grass.finding.mapper.UserMapper;
import com.grass.finding.utils.WXUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
* @author 20158
* @description 针对表【user(用户表)】的数据库操作Service实现
* @createDate 2023-02-09 22:44:49
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService {
    @Resource
    UserMapper userMapper;

    /**
     * 用户登录
     *
     * @param code       js_code 用来获取openID的
     * @param userAvatar
     * @param userName
     * @param gender
     * @return User对象
     */
    @Override
    public User login(String code, String userAvatar, String userName, String gender) {
        String openId = WXUtil.getOpenID(code);
        if(openId == null){
            throw new RuntimeException("获取openID失败");
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openId", openId);
        User user = userMapper.selectOne(queryWrapper);
        // 用户之前登录过, 则直接登录
        if(user != null){
            return user;
        }
        // 用户之前没有登录过
        user = new User();
        user.setOpenId(openId);
        user.setUserAvatar(userAvatar);
        user.setGender(gender);
        user.setUserName(userName);
        int insert = userMapper.insert(user);
        if(insert == 0){
            throw new RuntimeException("第一次登录时向数据库插入失败");
        }

        return user;
    }

    @Override
    public Integer modifyOneColumnString(String columnName, String columnValue, String openId) {
        // 规定修改范围，保证系统安全
        if("userName".equals(columnName) || "userPhone".equals(columnName)
                || "gender".equals(columnName) || "userBirthday".equals(columnName)
                || "userAddress".equals(columnName) || "userAvatar".equals(columnName)){
            // 用反射给user赋值
            Class<User> clazz = User.class;
            User user = new User();
            if("userBirthday".equals(columnName)){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    user.setUserBirthday(sdf.parse(columnValue));
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
            }
            else{
                try {
                    Field declaredField = clazz.getDeclaredField(columnName);
                    declaredField.setAccessible(true);
                    declaredField.set(user, columnValue);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }

            // 查询某一行
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("openId", openId);
            // 执行数据库修改
            int update = userMapper.update(user, queryWrapper);
            if(update == 0){
                throw new RuntimeException("修改失败");
            }
            return update;
        }else {
            throw new BusinessException(ErrorCode.INVALID_PARAMETER);
        }
    }

    @Override
    public User getUserInfoByOpenId(String openId) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openId", openId);
        User user = userMapper.selectOne(queryWrapper);
        if(user == null){
            log.error("没有通过openId查找到user对象");
            throw new RuntimeException("系统内部异常");
        }
        return user;
    }
}




